use crate::http::server::error::Error;
use crate::http::server::handler::result::IntoJsonResponseResult;
use crate::http::server::middleware::ValidatedJson;
use crate::{controller, dto};
use axum::Json;
use axum::extract::State;
use std::sync::Arc;

pub struct SegmentListHandler {
    controller_set: Arc<controller::ControllerSet>,
}

impl SegmentListHandler {
    pub fn new(controller_set: Arc<controller::ControllerSet>) -> Self {
        SegmentListHandler { controller_set }
    }

    pub async fn add(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::segment_list::AddRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::core::segment_list::AddResponseData>>, Error>
    {
        state
            .controller_set
            .segment_list
            .add(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn list_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::segment_list::ListRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::segment_list::ListInfoResponseData>>, Error>
    {
        state
            .controller_set
            .segment_list
            .list_info(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn get_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::segment_list::GetRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::segment_list::SegmentListInfo>>, Error>
    {
        state
            .controller_set
            .segment_list
            .get_info(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn update(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::segment_list::UpdateRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .segment_list
            .update(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn delete(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::segment_list::DeleteRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .segment_list
            .delete(&request.0)
            .await
            .into_json_response_result()
    }
}
